import { useRef } from 'react';

function useDoubleClick(maxGap = 300, minGap = 0) {
  const lastRef = useRef(0);

  return (callback) => (e) => {
    const currentTime = e.timeStamp;
    const gap = currentTime - lastRef.current;

    if (gap > minGap && gap < maxGap && !!callback) {
      callback(e);
    }

    lastRef.current = currentTime;
  };
}

export { useDoubleClick };
